package com.cskaoyan.market.service.impl;


import com.cskaoyan.market.bo.CategoryCreateBo;
import com.cskaoyan.market.bo.CategoryDeleteBo;
import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.db.domain.MarketCategoryExample;
import com.cskaoyan.market.db.mapper.MarketCategoryMapper;
import com.cskaoyan.market.service.AdminCategoryService;
import com.cskaoyan.market.util.Constant;
import com.cskaoyan.market.vo.MarketCategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminCategoryServiceImpl implements AdminCategoryService {

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Override
    public List<MarketCategory> l1() {
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        List<MarketCategory> categoryList = marketCategoryMapper.selectByExample(marketCategoryExample);
        List<MarketCategory> categoryList1 = new ArrayList<>();
        for (MarketCategory category : categoryList) {
            if ("L1".equals(category.getLevel())) {
                categoryList1.add(category);
            }
        }
        return categoryList1;
    }

    @Override
    public List<MarketCategoryVO> list() {
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        List<MarketCategory> categoryList = marketCategoryMapper.selectByExample(marketCategoryExample);
        Map<Integer, List<MarketCategory>> categoryGroupByPidMap = groupByPid(categoryList);
        List<MarketCategory> bigCategoryList = categoryGroupByPidMap.get(Constant.PROVINCE_TYPE);
        List<MarketCategoryVO> bigCategoryVoList = new ArrayList<>();
        for (MarketCategory bigCategory : bigCategoryList) {
            MarketCategoryVO bigCategoryVO = new MarketCategoryVO(bigCategory.getId(),bigCategory.getId(), bigCategory.getName(), bigCategory.getKeywords(), bigCategory.getDesc(), bigCategory.getIconUrl(), bigCategory.getPicUrl()
                    , bigCategory.getLevel(), bigCategory.getSortOrder(), bigCategory.getAddTime(), bigCategory.getUpdateTime(), bigCategory.getDeleted(), null);
            List<MarketCategoryVO> midCategoryVoList = new ArrayList<>();
            List<MarketCategory> midCategoryList = categoryGroupByPidMap.get(bigCategory.getId());
            if (midCategoryList != null){
                for (MarketCategory midCategory : midCategoryList) {
                    MarketCategoryVO midCategoryVo = new MarketCategoryVO(bigCategory.getId(), midCategory.getId(), midCategory.getName(),
                            midCategory.getKeywords(), midCategory.getDesc(), midCategory.getIconUrl(), midCategory.getPicUrl()
                            , midCategory.getLevel(), midCategory.getSortOrder(), midCategory.getAddTime(), midCategory.getUpdateTime(), midCategory.getDeleted(), null);
                    midCategoryVoList.add(midCategoryVo);

                }
            }
            bigCategoryVO.setChildren(midCategoryVoList);
            bigCategoryVoList.add(bigCategoryVO);
        }
        return bigCategoryVoList;
    }


    private Map<Integer, List<MarketCategory>> groupByPid(List<MarketCategory> categoryList) {
        Map<Integer,List<MarketCategory>> categoryGroupByPidMap = new HashMap<>();
        for (MarketCategory marketCategory : categoryList) {
            Integer pid = marketCategory.getPid();
            //session域存取数据的原理是一致的，之前的购物车案例
            //第一次去get的时候，肯定是null
            List<MarketCategory> categoryListOfPid = categoryGroupByPidMap.get(pid);
            if (categoryListOfPid == null) {
                categoryListOfPid = new ArrayList<>();
                categoryGroupByPidMap.put(pid, categoryListOfPid);
            }
            categoryListOfPid.add(marketCategory);

        }
        return categoryGroupByPidMap;
    }


    @Override
    public MarketCategory create(CategoryCreateBo categoryCreateBo){
        String name = categoryCreateBo.getName();
        String desc = categoryCreateBo.getDesc();
        String keywords = categoryCreateBo.getKeywords();
        String level = categoryCreateBo.getLevel();
        String iconUrl = categoryCreateBo.getIconUrl();
        String picUrl = categoryCreateBo.getPicUrl();
        String pid = categoryCreateBo.getPid().toString();
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setName(name);
        marketCategory.setDesc(desc);
        marketCategory.setKeywords(keywords);
        marketCategory.setLevel(level);
        marketCategory.setPid(Integer.parseInt(pid));
        marketCategory.setPicUrl(picUrl);
        marketCategory.setIconUrl(iconUrl);
        //时间格式转换
        marketCategory.setAddTime(LocalDateTime.now());
        marketCategory.setUpdateTime(LocalDateTime.now());
        marketCategoryMapper.insert(marketCategory);
        return  marketCategory;
    }

    @Override
    public void delete(String id) {
        marketCategoryMapper.deleteByPrimaryKey(Integer.parseInt(id));
    }

    @Override
    public void update(CategoryDeleteBo categoryDeleteBo) {
        String name = categoryDeleteBo.getName();
        String desc = categoryDeleteBo.getDesc();
        String iconUrl = categoryDeleteBo.getIconUrl();
        String keywords = categoryDeleteBo.getKeywords();
        String level = categoryDeleteBo.getLevel();
        String picUrl = categoryDeleteBo.getPicUrl();
        Integer pid = categoryDeleteBo.getPid();
        Integer id = categoryDeleteBo.getId();
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setName(name);
        marketCategory.setPid(pid);
        marketCategory.setIconUrl(iconUrl);
        marketCategory.setKeywords(keywords);
        marketCategory.setDesc(desc);
        marketCategory.setLevel(level);
        marketCategory.setPicUrl(picUrl);
        marketCategory.setId(id);
        marketCategoryMapper.updateByPrimaryKey(marketCategory);
    }
}
